import allure
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

from automation_ui.untils.log_util import logger
from automation_ui.untils.untils import Until


class BasePage:
    def __init__(self, driver: WebDriver = None):
        """
        初始化driver
        :param driver:
        """
        if driver is None:
            service = Service(executable_path=ChromeDriverManager().install())
            self.driver = webdriver.Chrome(service=service)
            self.driver.maximize_window()
            self.driver.implicitly_wait(9)
        else:
            self.driver = driver

    def find_ele(self, by, value=None):
        """
        查找单个元素
        :param by:
        :param value:
        :return:
        """
        try:
            if value is None:
                res = self.driver.find_element(*by)
            else:
                res = self.driver.find_element(by=by, value=value)
        except Exception as e:
            res = None
            logger.info("元素未找到", e)

        return res

    def find_eles(self, by, value=None):
        """
        查找多个元素
        :param by:
        :param value:
        :return:
        """
        try:
            if value is None:
                res = self.driver.find_elements(*by)
            else:
                res = self.driver.find_elements(by=by, value=value)
        except Exception as e:
            res = None
            logger.info("元素未找到", e)

        return res

    def send_key(self, by, key, value=None):
        """
        查找元素并发送文本
        :param by:
        :param value:
        :return:
        """
        try:
            if value is None:
                self.driver.find_element(*by).send_keys(key)
            else:
                self.driver.find_element(by=by, value=value).send_keys(key)
        except Exception as e:
            logger.info("元素未找到", e)

    def click(self, by, value=None):
        """
        查找元素并点击
        :param by:
        :param value:
        :return:
        """
        try:
            if value is None:
                self.driver.find_element(*by).click()
            else:
                self.driver.find_element(by=by, value=value).click()
        except Exception as e:
            logger.info("元素未找到", e)

    def clear(self, by, value=None):
        """
        清空文本
        :param by:
        :param value:
        :return:
        """
        try:
            if value is None:
                self.driver.find_element(*by).clear()
            else:
                self.driver.find_element(by=by, value=value).clear()
        except Exception as e:
            logger.info("元素未找到", e)

    def wait(self, method, time=8):
        """
        显示等待
        :return:
        """
        wait = WebDriverWait(self.driver, time).until(method)
        return wait

    def url(self, url):
        """
        打开页面
        :param url:
        :return:
        """
        self.driver.get(url)

    def quit(self):
        """
        退出浏览器
        :return:
        """
        self.driver.quit()

    def screencap(self):
        """
        截图
        :return:
        """
        screen_path = Until.save_source_dates('images')
        self.driver.save_screenshot(screen_path)
        allure.attach.file(
            screen_path,
            name='元素截图',
            attachment_type=allure.attachment_type.PNG
        )
        return screen_path

    def source(self):
        """
          源码
          :return:
          """
        source_path = Until.save_source_dates('pagesource')
        with open(source_path, "w", encoding='utf-8') as f:
            f.write(self.driver.page_source)
        logger.info(f'源码保存路径为{source_path}')
        allure.attach.file(
            source_path,
            name='源码',
            attachment_type=allure.attachment_type.TEXT
        )
        return source_path

    def refresh(self):
        """
        刷新页面
        :return:
        """
        self.driver.refresh()
